This question has been flagged
3 Replies
6822 Views

I would like to play a sound every time a custom button is pressed. The button is calling a server.action to update a custom field on an order and will be use to inform a user that the order is ready to be delivered.  I would like to use pyglet with this simple script:

#!/usr/bin/env python

import pyglet
wavfile = '/home/effe/theetone.wav' sound = pyglet.media.load(wavfile) core = pyglet.media.Player() core.queue(sound) core.play()

But is not working when I use a python expression on server.actions.

ValueError: opcode IMPORT_NAME not allowed (u"import pyglet\n\nwavfile = '/home/effe/theetone.wav'\nsound = pyglet.media.load(wavfile)\ncore = pyglet.media.Player()\ncore.queue(sound)\ncore.play()") 

Seems I can't declare an import (import pyglet) on python expression field and even if I put the import line on my module seems not to works. Where am I wrong?

 

Avatar
Discard
Best Answer

Hello,

I think import will not work in the code section of the server action ...

you can make a new function in your model and call it from the action, model.py :


def play_sound(self, cr, uid, context=None):
    myfile = '/home/ahmed/Music/bell.ogg'
    sound = pyglet.media.load(myfile)
    core = pyglet.media.Player()
    core.queue(sound)
    core.play()
    return True


Then from the action you can call this function:


<record model="ir.actions.server" id="play_sound_action">
    <field name="name">Play Server Action</field>
    <field name="model_id" ref="model_test_test"/>
    <field name="code">
           #
           # you code here
           #
    self.play_sound(cr, uid, context)
</field>
</record>


Regards ..

Avatar
Discard
Author

I've added on my modified point_of_sale.py the import and inherit the pos.order model:

class pos_order(osv.Model):
    _inherit = 'pos.order'

def play_sound(self, cr, uid, context=None):
    myfile = '/home/effe/KDE_Beep_Digital_1.ogg'
    sound = pyglet.media.load(myfile)
    core = pyglet.media.Player()
    core.queue(sound)
    core.play()
    return True
The update of pos.order did work well but I can't figure out how to implement the python code directly on gui... I've added directly the line
self.play_sound(cr, uid, context)
but obviously didn't work:
ValueError: "'pos.order' object has no attribute 'play_sound'" while evaluating
u'self.play_sound(cr, uid, context)'
Where I'm wrong?

Hi, where did you added the self.play_sound(cr, uid, context) ?

Author Best Answer

Ahmed,

since I got some issues even on pycharm I've update my script using pygame instead of pyglet because I need to separate the audio channels :

import pygame
import time

pygame.init()
sound = pygame.mixer.Sound("/home/effe/theetone.wav")
channel = sound.play()
#depending of the sound use left or right channel to mute
channel.set_volume(1,1)
time.sleep(2)

Works fine on the IDE but I'm still unable to correctly create a function on point_of_sale.py and link it to the server.action via gui on Odoo.

Avatar
Discard

Great!, I'll try it on point_of_sale.py whenever I have spare time ... It will be helpful if you can tell me the scenario, because I think you're working on POS ... and may be will need to call the function from js

Author

Amhed, thanks for your time.
Scenario:
I'm working on a new module for my restaurant. Since we have just one thermal printer and sincerely I don't like how Odoo manages printers, I started to create a new flow using a server, a client on the kitchen and a tablet to make the orders.
The server is located between the kitchen and the front desk and has one speaker for each ambient. When I send the order to the kitchen the server will to play a sound on the right speaker

channel.set_volume(0,1)
and this function should be implemented on pos js as you propose (how is another story).
When an order is done and the button linked to the actions.server on my form view will be pressed, another different sound will be sent to left speaker
channel.set_volume(1,0)
That will be the def I need on my .py.
I'm already using this method (without sounds linked) and works fine for my needs but have an alert will be grab the attention when there are many customers. When all will be ready I'll pack a module and share with the community because there are many missing things for a restaurant implementation on stock Odoo. Think on pos_restaurant module for example: if you haven't an ESC/POS compatible printer is pretty unhelpful because the module can't generate pdf or interactive views.

Hi, I think in Odoo 9 there will be a new POS for restaurants : https://docs.google.com/spreadsheets/d/18CZGaF7igfVEt5_P-F7KzmXPfc-XYXT0iaPFYldCkm0/edit#gid=0 anyway I think the second part is working with the script in the answer right ?

Author

Yes, a new pos with (finally) tables management. What second part you're talking about? I'm still stuck but for now I'm not working on pos 9 because is unstable. On production with use V8.

Hi, the second part is : when the order is done "and the button linked to the actions.server" ... - the first part is when you send the order to the kitchen ...

Author

The script that send the order to the kitchen works, but not the sound.

FEDERICO LEONI My question is that have any idea for the play sound in the single system not in the server, actually, my question is that I have 10 scanner machine so that time scann product if product not found any 1 system then play the sound so how I can archive